#!/usr/bin/env python
# encoding: utf-8

Import('env')
Import('create_uninstall_target')
Import('VERSION_MAJOR')
Import('VERSION_MINOR')
Import('VERSION_PATCH')

import os
import subprocess

if env['HAVE_MSGFMT']:
    for src in env.Glob('*.po'):
        lang = os.path.basename(src.get_abspath()[:-3])
        dest = lang + '.mo'

        cmd = env.AlwaysBuild(
            env.Command(dest, src, 'msgfmt $SOURCE -o $TARGET')
        )

        path = '$PREFIX/share/locale/%s/LC_MESSAGES/rmlint.mo' % lang

        if 'install' in COMMAND_LINE_TARGETS:
            target = env.InstallAs(path, dest)
            env.Depends(target, cmd)
            env.Alias('install', [target])

        if 'uninstall' in COMMAND_LINE_TARGETS:
            create_uninstall_target(env, path)


def xgettext():
    return subprocess.call(
        'xgettext --package-name rmlint -k_ -kN_ ' \
        '--package-version {}.{}.{} --default-domain rmlint ' \
        '--add-location=file ' \
        '--join-existing ' \
        '--output po/rmlint.pot ' \
        '--from-code UTF-8 ' \
        '$(find src lib -iname "*.[ch]") &&' \
        'sed -i "s/CHARSET/UTF-8/g" po/rmlint.pot'.format(
            VERSION_MAJOR,
            VERSION_MINOR,
            VERSION_PATCH
        ),
        shell=True
    )


def xgettext_action(target=None, source=None, env=None):
    status = xgettext()
    if status:
        print('Error: xgettext failed')
        Exit(status)


def msgmerge(target=None, source=None, env=None):
    return subprocess.call(
        'set -e;' \
        'for p in po/*.po; do' \
        '  msgmerge --update --backup=none "$p" po/rmlint.pot;' \
        'done',
        shell=True
    )


def msgmerge_action(target=None, source=None, env=None):
    status = msgmerge()
    if status:
        print('Error: msgmerge failed')
        Exit(status)


def gettext_action(target=None, source=None, env=None):
    status = xgettext()
    if status:
        ('Error: gettext failed while running xgettext')
        Exit(status)
    status = msgmerge()
    if status:
        print('Error: gettext failed while running msgmerge')
        Exit(status)


if 'xgettext' in COMMAND_LINE_TARGETS:
    env.Alias(
        'xgettext',
        env.Command('xgettext', None, Action(xgettext_action, "Running xgettext"))
    )

if 'msgmerge' in COMMAND_LINE_TARGETS:
    env.Alias(
        'msgmerge',
        env.Command('msgmerge', None, Action(msgmerge_action, "Running msgmerge"))
    )

if 'gettext' in COMMAND_LINE_TARGETS:
    env.Alias(
        'gettext',
        env.Command('gettext', None, Action(gettext_action, "Running gettext"))
    )
